<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 搜索内容与添加商品 -->

            <el-row :gutter="20">
                <el-col :span="6">
                    <el-input
                        placeholder="请输入搜索内容"
                        v-model="queryInfo.query"
                        @keyup.enter.native="getGoodsList"
                        clearable
                        @clear="getGoodsList"
                    >
                        <el-button
                            slot="append"
                            icon="el-icon-search"
                            @click="getGoodsList"
                        ></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button
                        type="primary"
                        @click="goAddpage"
                    >添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 商品列表区域 -->
            <el-table
                :data="goodList"
                border
                stripe
            >
                <el-table-column
                    type="index"
                    label="序号"
                    align="center"
                >
                </el-table-column>
                <el-table-column
                    prop="goods_name"
                    label="商品名称"
                    header-align="center"
                ></el-table-column>
                <el-table-column
                    prop="goods_price"
                    label="商品价格(元)"
                    align="center"
                    width="95"
                ></el-table-column>
                <el-table-column
                    prop="goods_weight"
                    label="商品重量"
                    align="center"
                    width="70"
                ></el-table-column>
                <el-table-column
                    label="创建时间"
                    align="center"
                    width="140"
                >
                    <template slot-scope="scope">
                        {{ scope.row.add_time |dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="editGoodsById(scope.row.goods_id)"
                        ></el-button>
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            @click="removeGoodsById(scope.row.goods_id)"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5,10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                background
            >
            </el-pagination>
        </el-card>
    </div>
</template>
 
<script>
export default {
    data() {
        return {
            // 获取商品列表的参数
            queryInfo: {
                query: '',
                pagenum: 1, //页码
                pagesize: 10 //页大小
            },
            // 商品列表
            goodList: [],
            // 总条数
            total: 0,
        }
    },
    created() {
        this.getGoodsList();
    },
    methods: {
        // 获取商品列表
        async getGoodsList() {
            const { data: res } = await this.$http.get('goods', {
                params: this.queryInfo,
            });
            if (res.meta.status !== 200) {
                return this.$message.error('获取用户列表失败');
            }
            this.goodList = res.data.goods;
            this.total = res.data.total;
        },
        // 处理分页大小
        handleSizeChange(pagesize) {
            this.queryInfo.pagesize = pagesize;
            // 刷新商品列表
            this.getGoodsList();
        },
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getGoodsList();
        },
        // 添加商品
        goAddpage() {
            this.$router.push('/goods/add');
        },
        // 
        async editGoodsById(id){
            this.$router.push({
				path:'/goods/edit',
				query: {
					id:id
				}
			});
        },
        // 删除商品
        removeGoodsById(id) {
            this.$confirm('确定要删除该商品吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const { data: res } = await this.$http.delete(`goods/${id}`);
                if (res.meta.status !== 200) {
                    return this.$message.error('删除商品失败');
                }
                this.getGoodsList();
                this.$message.success('删除商品成功');
            }).catch(() => {
                this.$message.info('已取消删除');
            });
        }
    },
}
</script>
 
<style lang="less" scoped>
</style>